import { app as f, BrowserWindow as P, ipcMain as p } from "electron";
import { fileURLToPath as R } from "node:url";
import e from "node:path";
import m from "node:fs";
const h = e.dirname(R(import.meta.url));
process.env.APP_ROOT = e.join(h, "..");
const w = process.env.VITE_DEV_SERVER_URL, v = e.join(process.env.APP_ROOT, "dist-electron"), _ = e.join(process.env.APP_ROOT, "dist");
process.env.VITE_PUBLIC = w ? e.join(process.env.APP_ROOT, "public") : _;
let i;
function u() {
  i = new P({
    icon: e.join(process.env.VITE_PUBLIC, "electron-vite.svg"),
    webPreferences: {
      preload: e.join(h, "preload.mjs"),
      nodeIntegration: !0,
      contextIsolation: !0,
      webSecurity: !1
      // 禁用同源策略
    }
  }), i.webContents.on("did-finish-load", () => {
    i == null || i.webContents.send("main-process-message", (/* @__PURE__ */ new Date()).toLocaleString());
  }), w ? (i.loadURL(w), i.webContents.openDevTools()) : i.loadFile("dist/index.html");
}
f.on("window-all-closed", () => {
  process.platform !== "darwin" && (f.quit(), i = null);
});
f.on("activate", () => {
  P.getAllWindows().length === 0 && u();
});
let c = f.getPath("home");
c = e.join(c, "ditu");
p.handle("read-json-file", (s, r) => {
  console.log(s);
  let l = e.join(c, r);
  return new Promise((a, o) => {
    m.readFile(l, "utf8", (n, t) => {
      if (n)
        o(n);
      else
        try {
          const d = JSON.parse(t);
          a(d);
        } catch (d) {
          o(d);
        }
    });
  });
});
p.handle("write-json-file", (s, r, l) => {
  console.log(s);
  let a = e.join(c, r);
  return new Promise((o, n) => {
    m.writeFile(a, JSON.stringify(l, null, 2), "utf8", (t) => {
      t ? n(t) : o(!0);
    });
  });
});
p.handle("read-image", (s, r) => {
  console.log(s);
  let l = e.join(c, r);
  return new Promise((a, o) => {
    m.readFile(l, "utf8", (n, t) => {
      if (n)
        o(n);
      else
        try {
          a(t);
        } catch (d) {
          o(d);
        }
    });
  });
});
p.handle("write-image", (s, r, l) => {
  console.log(s);
  let a = e.join(c, r);
  return new Promise((o, n) => {
    m.writeFile(a, l, (t) => {
      t ? n(t) : o(!0);
    });
  });
});
f.whenReady().then(u);
export {
  v as MAIN_DIST,
  _ as RENDERER_DIST,
  w as VITE_DEV_SERVER_URL
};
